/*发布食谱*/
$(function () {
    /**
     * 增加食材
     */
    $('.op-food.add').click(function () {
        var box = $(this).parent().parent().parent();
        //id前缀
        var idPrefix = $(box).attr('id');
        var lastFood = $('#' + idPrefix + ' .food').last();

        var input = $(lastFood).find('input');
        var listArr = $(input).attr('list').split('_');
        var listIndex = parseInt(listArr[1]) + 1;
        var idIndex = listArr[0] + '_' + listIndex;
        //添加节点
        var currentFood = $(this).parent().parent();
        var clone = $(lastFood).clone(true);
        $(clone).find('input').val('');
        $(currentFood).after(clone);
        //找到增加的节点
        var cloneFood = $('#' + idPrefix + ' .food').last();
        //修改list 和id值
        $(cloneFood).find('input').attr('list', idIndex);
        $(cloneFood).find('datalist').attr('id', idIndex);
    });

    /**
     * 删除食材
     */
    $('.op-food.remove').click(function () {
        var box = $(this).parent().parent().parent();
        //id前缀
        var idPrefix = $(box).attr('id');
        var foodLength = $('#' + idPrefix).find('.food').length;
        if (foodLength <= 1) {
            return;
        }
        var food = $(this).parent().parent();
        $(food).remove();
    });

    /**
     * 增加步骤
     */
    $('.op-step .add').click(function () {
        var step = $(this).parent().parent();
        var clone = $(step).clone(true);
        //清除
        $(clone).find('.stepcontent').val("");
        $(clone).find('.step-img').attr('src', '');
        //添加
        $(step).after(clone);
        stepSort();
    });


    /**
     * 删除步骤
     */
    $('.op-step .remove').click(function () {
        var length = $('#steps').find('.step').length;
        if (length <= 1) {
            return;
        }
        var step = $(this).parent().parent();
        $(step).remove();
        stepSort();
    });


    /**
     * 图片预览
     */
    $('.inputStepimg').change(function () {
        var read = new FileReader(); // 创建FileReader对像;
        read.readAsDataURL(this.files[0]);  // 调用readAsDataURL方法读取文件;
        var _this = this;
        read.onload = function () {
            url = read.result;  // 拿到读取结果;
            var img = $(_this).next();
            $(img).attr("src", url);
        }
    });

    /**
     * 成品图
     */
    $('.cover-img').change(function () {
        var read = new FileReader(); // 创建FileReader对像;
        read.readAsDataURL(this.files[0]);  // 调用readAsDataURL方法读取文件;
        var _this = this;
        read.onload = function () {
            url = read.result;  // 拿到读取结果;
            var img = $(_this).next();
            $(img).attr("src", url);
        }
    });

    var foodList;
    var data = {};
    $.getJSON('/foodList', function (result) {
        foodList = result;
        for(var i = 0; i < foodList.length; i++) {
            var foodId = foodList[i].id;
            var foodName = foodList[i].food;
            var option = '<option data-value="'+foodId+'" value="'+foodName+'"/>';
            $('#mainFoodList_0').append(option);
            data[foodName] = foodId;
        }
    });


    $('#publish').click(function () {
        var formData = new FormData();
        //基本信息
        var cookbookname = $('#cookbookname').val();
        formData.append("cookbookname", cookbookname);

        var appropriatecrowd = $('#appropriatecrowd').val();
        formData.append("appropriatecrowd", appropriatecrowd);

        var style = $('#style').val();
        formData.append("style", style);

        var craft = $('#craft').val();
        formData.append("craft", craft);

        var taste = $('#taste').val();
        formData.append("taste", taste);

        var duration = $('#duration').val();
        formData.append("duration", duration);

        var difficulty = $('#difficulty').val();
        formData.append("difficulty", difficulty);

        //成品图
        var coverimg = $('#coverimg').attr('src');
        formData.append("coverimg", coverimg);

        //食材
        var inputFoodList = $('.input-food');
        var foodStrList = {};
        for(var i = 0; i < inputFoodList.length; i++) {
            var inputFood = $(inputFoodList[i]).val();
            if(data[inputFood] != undefined) {
                formData.append("food"+i, data[inputFood]);
            }
        }

        //步骤
        var stepList = $('.step');
        var stepcontentStrList = {};
        var stepimgStrList = {};
        for(i = 0; i < stepList.length; i++) {
            var stepcontent = $(stepList[i]).find('.stepcontent').val();
            var stepimg = $(stepList[i]).find('.stepimg').attr('src');
            formData.append("stepcontent"+i, stepcontent);
            formData.append("stepimg"+i, stepimg);
        }
        $.ajax({
            url: '/publishCookBookInfo' ,
            type: 'post',
            data: formData,
            cache: false,
            processData: false,
            contentType: false,
            async: true,
            success : function (result) {
                closeLoading();
                if(result.code == -1) {
                    winTip('会话超时,请重新登录！', true);
                    window.location = "/login";
                    return ;
                } else if(result.code == 1) {
                    winTip('食谱发布成功', true);
                    $('#winBtn').click(function () {
                        window.location = "/index";
                    });
                } else {
                    winTip('食谱发布失败', false);
                    $('#winBtn').click(function () {
                        $('#winTip').hide();
                    });
                }
            }
        });
    });

});

/**
 * 步骤排序
 */
function stepSort() {
    var step = $('.step').children('.step-no');
    for (var i = 0; i < step.length; i++) {
        $(step[i]).html(i + 1);
    }
}


